import config from "../../config";
import { createSlice } from "@reduxjs/toolkit";

const sessionTheme = JSON.parse(
  localStorage.getItem(config.SESSION_THEME)
);
const initTheme = sessionTheme ? sessionTheme : config.initTheme;
const initialState = {
  dark: initTheme.dark,
  colorPrimary: initTheme.colorPrimary,
};

export const themeSlice = createSlice({
  name: "theme",
  initialState,
  reducers: {
    setDark(state, action) {
      state.dark = action.payload;
      localStorage.setItem(config.SESSION_THEME, JSON.stringify(state));
    },
    setColorPrimary(state, action) {
      state.colorPrimary = action.payload;
      localStorage.setItem(config.SESSION_THEME, JSON.stringify(state));
    },
  },
});

export const { setDark } = themeSlice.actions;
export const { setColorPrimary } = themeSlice.actions;

export default themeSlice.reducer;
